<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();

define('HOME_ID', 56);

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$art_id = MiscUtils::getParam('art_id', 0);
$cat_id = MiscUtils::getParam('cat_id', 0);
$script = MiscUtils::getParam('script', 'index');

$cond_vals = new stdClass();
$cond_vals->c = 't.id =  :v1';
$cond_vals->v = array(':v1' => $art_id);
$opts = new stdClass();
$opts->select_expr = 'Distinct t.id, t.title, t.date, t.content';
$details = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, NULL, NULL, 1, 1, $opts)->d;

//
if (isset($_COOKIE['news_read'])) {
  $_SESSION['news_read'] = json_decode($_COOKIE['news_read']);
}

if (!isset($_SESSION['news_read'])) {
  $_SESSION['news_read'] = array();
}

$is_exist = false;
foreach ($_SESSION['news_read'] as $obj) {
  if ($obj->id == $art_id) {
    $is_exist = true;
  }
}

if (!$is_exist) {
  if (count($_SESSION['news_read']) >= 10) {
    array_pop($_SESSION['news_read']);
  }
  $details[0]->_cat_id = $cat_id;
  $_SESSION['news_read'][] = $details[0];
  setcookie('news_read', json_encode($_SESSION['news_read']), time() + 86400 * 7);
}

//
$myPdo->exec('UPDATE ' . TABLE_NAME_PREFIX_PLUGIN . 'art_post SET visitors = visitors+1 where id=' . $art_id);

//
$tags = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

$cond_vals = new stdClass(); 
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $cat_id);
$opts = new stdClass();
$opts->select_expr = 't.id, t.parent_id, t.name';
$tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', $cond_vals, 't.order', 'ASC', NULL, NULL, $opts)->d;

$rslt2 = new stdClass();
$rslt2->parents = array();
if (count($tmps) > 0) {
  $tmp = $tmps[0];
  $rslt2->parents[] = $tmp;
  _getParentTags($tags, $tmp, $rslt2);
  rsort($rslt2->parents);
}


function _getParentTags($arr, $tmp, $rslt) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $obj = $arr[$i];
    if (isset($tmp) && $tmp->parent_id == $obj->id) {
      $rslt->parents[] = $obj;
      _getParentTags($arr, $obj, $rslt);
    }
  }
}

?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<style>
.form {
  width: 98%;
  min-height: 300px;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  font-weight: normal;
  vertical-align: middle;
  padding: 5px;
}

.detail_title {
  text-align: center;
  vertical-align: middle;
  font-size: 24px;
  line-height: 2;
  margin: 0;
  padding: 0;
  font-family: Lucida Sans Unicode;
  font-weight: bold;
}
.detail_date {
  color: #666;
  line-height: 2;
  padding-bottom: 6px;
  text-align: center;
  vertical-align: middle;
}
.detail_content{
  padding: 15px 30px 50px 10px;
  line-height: 20px;
  text-align: left;
  vertical-align: top;
  font-family: Lucida Sans Unicode;
  font-size: 13px;
  width: 90%;
  min-height: 250px;
}

p{
  margin: 0;
  -webkit-margin-before: 0px;
-webkit-margin-after: 0px;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
}

.list_nav {
  margin: 0 0 5px 0;
  border: solid 1px #ddd;
  background-color: #fff;
  height: 33px;
  padding-left: 10px;
}

ul.nav {
  margin: 0;
  padding: 0;
  text-align: left;
  vertical-align: middle;
  font: 12px/1.6 Verdana, Arial, Helvetica, sans-serif, "宋体";
}
ul.nav li {
  display: inline;
}
ul.nav li.gap {
  padding: 0 5px;
}
ul.nav a {
  text-decoration: none;
  color: #333;
}
ul.nav a:hover {
  text-decoration: underline;
  color: #666;
}
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0 style="width: 100%">
  <tr>
    <td align="center" valign="top">
      <table width="100%" cellpadding=0 cellspacing=0 class="list_nav">
        <tr><td>
          <ul class="nav">
            <li>Your Position: &nbsp;&nbsp;</li>
            <li><a href="#" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . HOME_ID; ?>'">Home</a></li>
            <li class="gap">&#8250;</li>
            <?php foreach($rslt2->parents as $idx => &$tag) { ?>
              <li><a href="#" onclick="parent.location.href = '<?php echo 'http://' . URL . '?i=' . $page_id . '&cat_id=' . $tag->id; ?>'"><?php echo $tag->name; ?></a></li>
              <?php if ($idx < count($rslt2->parents) - 1) { ?>
              <li class="gap">&#8250;</li>
              <?php } ?>
            <?php } ?>
             <?php if (count($rslt2->parents) == 0) {?>
              <li>Undefined</li>
            <?php }?>
          </ul>
        </td></tr>
      </table>
      <div style="border: 1px solid #ddd;" align="center" valign="top">
        <table cellpadding=0 cellspacing=0 class="form">
          <tr>
            <td style="height: 50px;">
			        <h1 class="detail_title" title="<?php echo $details[0]->title;?>">
			           <?php echo MiscUtils::getShortName($details[0]->title, 40) ;?>
			        </h1>
			        <div class="detail_date" >
			          <?php echo $details[0]->date;?>
			        </div>
			        <div style="width: 100%; border-bottom: dashed 1px #ddd "></div>
			      </td>
			    </tr>
			    <tr>
            <td class="detail_content" style="height: 250px;">
               <?php echo $details[0]->content ;?>
            </td>
          </tr>
        </table>
      </div>
    </td>
    <td  align="left" valign="top" style="width: 20px">
    <td  align="left" valign="top" style="width: 300px">
      <div>
        <?php include dirname(__FILE__) . '/inc.right.php'; ?>
      </div>
    </td>
  </tr>
</table>
</body>
</html>
